/**  * Date：2011/07/15  * 功能：好友列表  */
package org.client.ui; 
import java.awt.GridLayout;   
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.util.*;

import javax.swing.*;  

import org.client.tools.ClientTool;
import org.client.view.manage.Client2ServerThread;
import org.pojo.*;
import org.pojo.Message.Message;
public class IMMainWnd extends JFrame {     

	/**
	 * 
	 */
	private static final long serialVersionUID = 1580482520692417203L;
	/*定义组件*/     //定义第一张卡片    
	private JPanel fjp1,friendJp,memberJp;         
	private JButton fjb1,fjb2,fjb3,fjb4,personInfo;
	private JTextField sign;
	private JScrollPane member; 
	private JScrollPane friend; 
	private JLabel headJb,jLabel0;
	private int userid;
	private IMMemberList memberlist;
	private IMFriendsList friendlist;


	public IMMainWnd(){   
		/*处理组件 */ 
		memberlist = new IMMemberList();
		friendlist = new IMFriendsList();
		sign = new JTextField("个性签名： " + ClientTool.currentUser.getSign());
		sign.setEditable(false);
		//Font font = new Font("方正喵呜体",Font.BOLD,20);                 
		fjp1 = new JPanel(null);
		ImageIcon backgrd = new ImageIcon("images/IM背景.png");
		jLabel0 = new JLabel(backgrd);
		jLabel0.setBounds(-3, 0, backgrd.getIconWidth(),backgrd.getIconHeight());
		this.getLayeredPane().add(jLabel0,new Integer(Integer.MIN_VALUE));
		JPanel jp = (JPanel) this.getContentPane();
		jp.setOpaque(false);
		fjp1.setOpaque(false);
		
		headJb = new JLabel(" "+ClientTool.currentUser.getNickname(),
				new ImageIcon("images/"+ClientTool.currentUser.getPhoto()),JLabel.LEFT);
		
		personInfo = new JButton("编辑信息");
		personInfo.addMouseListener(new editListener());
		
		//fjp1 = new JPanel(new BorderLayout()); 	   
		memberJp = new JPanel(new GridLayout(10, 1, 4, 4));
		friendJp = new JPanel(new GridLayout(10, 1, 4, 4));
		fjb1 = new JButton("↑在线成员");
		//fjb1.setFont(font);
		fjb1.addMouseListener(new fjbListener(this));
		//fjb1.setForeground(Color.BLUE);  
		fjb2 = new JButton("↑我的好友");
		//fjb2.setFont(font);
		fjb2.addMouseListener(new fjbListener(this));
		//fjp3.add(fjb1);          
		//fjp3.add(fjb2);
		
		member = new JScrollPane(memberJp);// 把fjp1放到可以滚动的JScrollPane里 
		friend = new JScrollPane(friendJp);
		
		headJb.setBounds(10, 10, 180, 50);	
		personInfo.setBounds(195, 40, 90, 22);
		sign.setBounds(10, 70, 276, 20);
		member.setBounds(6, 105, 283, 385);
		friend.setBounds(6, 139, 283, 385);
		fjb1.setBounds(0, 500, 294, 34);
		fjb2.setBounds(0, 534, 294, 34);
		
		member.show();
		friend.hide();
		
		fjp1.add(headJb);
		fjp1.add(personInfo);
		fjp1.add(sign);
		fjp1.add(member);
		fjp1.add(friend);
		fjp1.add(fjb1);  
		fjp1.add(fjb2);

		this.add(fjp1);       
		this.setIconImage(Toolkit.getDefaultToolkit().createImage("images/im.png"));
		this.setTitle("IM2012Beta");             
		this.setSize(300, 600);
		this.addWindowListener(new winAdapter());       
		this.setVisible(true); 
		this.setResizable(false); 
	} 
	
	public void updateOnlineFriends(List<User> u)
	{
		memberlist.updateOnline(u,memberJp);
		this.validate();
	}
	public void updateOfflineFriends(User u){
		memberlist.updateOffLine(u,memberJp);
		memberJp.repaint();
		this.validate();
	}
	public void updateExistFriend(){
		memberlist.updateExist(memberJp);
		memberJp.repaint();
		this.validate();
	}
	public void updateMyInfo(){
		headJb.setText(ClientTool.currentUser.getNickname());
		sign.setText("个性签名： " + ClientTool.currentUser.getSign());
		this.validate();
	}
	public void AcceptChat(Message m){
		memberlist.AcceptChat(m);
	}
	class winAdapter extends WindowAdapter{
		public void windowClosing(WindowEvent windowEvent){
			
			Message m = new Message();
			m.setMesType(MessageType.message_closesocket);//请求socket断开 
			try {
				//ObjectOutputStream oos = new ObjectOutputStream(ClientConServerThread.s.getOutputStream());
				Client2ServerThread.oos.writeObject(m);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.exit(0);
		}
	}
	class fjbListener extends MouseAdapter{
		private JFrame jf;
		public fjbListener(Object o){
			this.jf =(JFrame)o;
		}
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			JButton lb = (JButton) e.getSource();
			if(lb.getText().equals("↑在线成员")){
				member.show();
				friend.hide();
				fjb1.setBounds(0, 500, 294, 34);
				jf.validate();
			}else if(lb.getText().equals("↑我的好友")){
				fjb1.setBounds(0, 100, 294, 34);
				member.hide();
				friend.show();
				jf.validate();
			}
		}
	}
	//编辑信息
	class editListener extends MouseAdapter{
		public void mouseClicked(MouseEvent e) {
			// TODO Auto-generated method stub
			new IMEditWnd();
		}
	}
	 public static void main(String[] args)
	 {         // TODO Auto-generated method stub        
			IMMainWnd friendlist = new IMMainWnd();
			//String[] tests = {"小明","李林"};
			//friendlist.upateFriend(tests);
	 } 
}